﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace SplendidCRM.Controls
{
    public class DataBind : Control, INamingContainer
    {
        public DataBind()
        {
            DataBinding += new EventHandler(DataBind_DataBinding);
        }

        [TemplateContainer(typeof (DataBind))]
        [TemplateInstance(TemplateInstance.Single)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate Template { get; set; }

        private void DataBind_DataBinding(object sender, EventArgs e)
        {
            _binded = true;
        }

        private Boolean _binded;

        protected override void OnPreRender(EventArgs e)
        {
            if (Template != null) Template.InstantiateIn(this);
            if (!_binded) DataBind();
            base.OnPreRender(e);
        }
    }
}